<?php
// $connect = mysql_connect('mysql17.000webhost.com','a4117838_simple','123simple');
$connect = mysql_connect('localhost','root','');
if (!$connect){
  die('Could not connect: ' . mysql_error($connect));
}
// mysql_select_db('a4117838_simple',$connect);
mysql_select_db('mydatabase',$connect);
$dataoption   = $_POST['dataoption'];

if($dataoption=='voucher')
{
    $voucher  = $_POST['voucher'];
    $email    = $_POST['email'];
    $result   = mysql_query('select * from tablelist');
    //print_r(mysql_num_rows($result));
    while ($row = mysql_fetch_array($result)) {
        //print_r($row[1]);
    
        $sql      = "SELECT first_name FROM ".$row[1]." where voucher_code='".$voucher."' and email_address='".$email."'";
        $result0   = mysql_query($sql);
        $num_rows = mysql_num_rows($result0);
        //print_r($num_rows);
        if($num_rows == 0)
        {
            //echo "not in ".$row[1];
        }
        else 
        {
            $sql1       = "SELECT id,question_full FROM question where tablename='".$row[0]."'";
            $result1    = mysql_query($sql1);
            while ($row1 = mysql_fetch_array($result1, MYSQL_NUM)) {
                echo json_encode($row1[0].",".$row1[1].",".$row[1]);
            }
            mysql_free_result($result1);
            break;
        }
    }  
}
else if($dataoption=='q1'){
    $option1      = $_POST['option1'];
    
    if($option1!="0")
    {
        $voucher_code = $_POST['voucher_code']; 
        $table_name   = $_POST['table_name'];
        $sql0     = "SELECT box_email_dropdown FROM question where question.id='".$option1."'";
        $result0  = mysql_query($sql0);
        $num_rows = mysql_num_rows($result0);
        if($num_rows==0)
            echo json_encode (false);
        else if($num_rows!=0)
        {
            while ($row0 = mysql_fetch_array($result0, MYSQL_NUM)) {
                $answeroption = $row0[0];
            }
        }
        if($answeroption == 'dropdown')
        {
            $sql1     = "SELECT subquestion.id,subquestion_full FROM subquestion,question where subquestion.question_id='".$option1."' AND subquestion.question_id=question.id ";
            $result   = mysql_query($sql1);
            $num_rows = mysql_num_rows($result);
            if($num_rows==0)
                echo json_encode (false);
            else
            {
                echo '<div class="control-group" id="question2"><label class="control-label" for="q2">Selecteer uw vraag:</label><div class="controls"><select class="input-xxlarge" id="q2" name="q2"><option value="">Maak uw keus</option>';
                while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                    //echo json_encode($row[0].",".$row[1]);                
                    echo '<option value="'.$row[0].'">'.$row[1].'</option>';
                }   
                echo '<option value="0">my question is not listed</option></select></div></div>';
            }
            mysql_free_result($result);
        }
        else if($answeroption == 'box')
        {
            // echo "in box";
            $userquery  = "SELECT * FROM $table_name where voucher_code='".$voucher_code."'";
            $userresult =  mysql_query($userquery);

            while($userrow = mysql_fetch_assoc($userresult)){
                $first_name     = $userrow['first_name'];
                $last_name      = $userrow['last_name'];
                $email_address  = $userrow['email_address'];
                $vendor_code    = $userrow['vendor_code'];
                $voucher_code   = $userrow['voucher_code'];
                $street_address = $userrow['street_address'];
                $house_number   = $userrow['house_number'];
                $zip_code       = $userrow['zip_code'];
                $city           = $userrow['city'];
                $purchased      = $userrow['purchased'];
            }

            $sql00      = "SELECT answer FROM answer where question_id='".$option1."'";
            $result00   = mysql_query($sql00);
            $num_rows   = mysql_num_rows($result00);
            if($num_rows==0)
                echo json_encode (false);
            else
            {
                while ($row00 = mysql_fetch_array($result00, MYSQL_NUM)) {

                    $message    = "\n\n".$row00[0];
                    /*$message    = str_replace("\n", PHP_EOL, $row00[0]);
                    $message    = str_replace("\r", PHP_EOL, $message);
                    $message    = str_replace("\r\n", PHP_EOL, $message);*/
                    $newmessage = str_replace("{first_name}", $first_name, $message);
                    $newmessage = str_replace("{last_name}", $last_name, $newmessage);
                    $newmessage = str_replace("{email_address}", $email_address, $newmessage);
                    $newmessage = str_replace("{vendor_code}", $vendor_code, $newmessage);
                    $newmessage = str_replace("{voucher_code}", $voucher_code, $newmessage);
                    $newmessage = str_replace("{street_address}", $street_address, $newmessage);
                    $newmessage = str_replace("{house_number}", $house_number, $newmessage);
                    $newmessage = str_replace("{zip_code}", $zip_code, $newmessage);
                    $newmessage = str_replace("{city}", $city, $newmessage);
                    $newmessage = str_replace("{purchased}", $purchased, $newmessage);

                    echo "<div id='msg1'><label>Answer:</label><div id='lastmsg'>".nl2br($newmessage)."</div></div>";
                }
            }
        }
        else if($answeroption == 'email')
        {
            $userquery  = "SELECT * FROM $table_name where voucher_code='".$voucher_code."'";
            $userresult =  mysql_query($userquery);

            while($userrow = mysql_fetch_assoc($userresult)){
                $first_name     = $userrow['first_name'];
                $last_name      = $userrow['last_name'];
                $email_address  = $userrow['email_address'];
                $vendor_code    = $userrow['vendor_code'];
                $voucher_code   = $userrow['voucher_code'];
                $street_address = $userrow['street_address'];
                $house_number   = $userrow['house_number'];
                $zip_code       = $userrow['zip_code'];
                $city           = $userrow['city'];
                $purchased      = $userrow['purchased'];
            }

            $sql00      = "SELECT answer FROM answer where question_id='".$option1."'";
            $result00   = mysql_query($sql00);
            $num_rows = mysql_num_rows($result00);
            if($num_rows==0)
                echo json_encode (false);
            else
            {
                while ($row00 = mysql_fetch_array($result00, MYSQL_NUM)) {

                    $message    = "\n\n".$row00[0];
                    /*$message    = str_replace("\n", PHP_EOL, $row00[0]);
                    $message    = str_replace("\r", PHP_EOL, $message);
                    $message    = str_replace("\r\n", PHP_EOL, $message);*/
                    $newmessage = str_replace("{first_name}", $first_name, $message);
                    $newmessage = str_replace("{last_name}", $last_name, $newmessage);
                    $newmessage = str_replace("{email_address}", $email_address, $newmessage);
                    $newmessage = str_replace("{vendor_code}", $vendor_code, $newmessage);
                    $newmessage = str_replace("{voucher_code}", $voucher_code, $newmessage);
                    $newmessage = str_replace("{street_address}", $street_address, $newmessage);
                    $newmessage = str_replace("{house_number}", $house_number, $newmessage);
                    $newmessage = str_replace("{zip_code}", $zip_code, $newmessage);
                    $newmessage = str_replace("{city}", $city, $newmessage);
                    $newmessage = str_replace("{purchased}", $purchased, $newmessage);

                    echo "<div id='msg1'><label>Answer:</label><div id='lastmsg'>".nl2br($newmessage)."</div></div>";
                }
            }
        }
    }
}
else if($dataoption=='q2'){
    $option2 = $_POST['option2'];
    $voucher_code = $_POST['voucher_code']; 
    $table_name   = $_POST['table_name'];
    
    $userquery  = "SELECT * FROM $table_name where voucher_code='".$voucher_code."'";
    $userresult =  mysql_query($userquery);
    while($userrow = mysql_fetch_assoc($userresult)){
            $first_name     = $userrow['first_name'];
            $last_name      = $userrow['last_name'];
            $email_address  = $userrow['email_address'];
            $vendor_code    = $userrow['vendor_code'];
            $voucher_code   = $userrow['voucher_code'];
            $street_address = $userrow['street_address'];
            $house_number   = $userrow['house_number'];
            $zip_code       = $userrow['zip_code'];
            $city           = $userrow['city'];
            $purchased      = $userrow['purchased'];
        }    

    $sql1    = "SELECT subanswer FROM subanswer where sub_question_id='".$option2."'";
    $result  = mysql_query($sql1);
    $num_rows= mysql_num_rows($result);
    if($num_rows==0)
        echo json_encode (false);
    else
    {
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

                $message    = "\n\n".$row[0];
                // $message    = preg_replace('/(\r\n\r\n)/', '', $row[0]);
                /*$message    = str_replace("\n", PHP_EOL, $row[0]);
                $message    = str_replace("\r", PHP_EOL, $message);
                $message    = str_replace("\r\n", PHP_EOL, $message);*/
                $newmessage = str_replace("{first_name}", $first_name, $message);
                $newmessage = str_replace("{last_name}", $last_name, $newmessage);
                $newmessage = str_replace("{email_address}", $email_address, $newmessage);
                $newmessage = str_replace("{vendor_code}", $vendor_code, $newmessage);
                $newmessage = str_replace("{voucher_code}", $voucher_code, $newmessage);
                $newmessage = str_replace("{street_address}", $street_address, $newmessage);
                $newmessage = str_replace("{house_number}", $house_number, $newmessage);
                $newmessage = str_replace("{zip_code}", $zip_code, $newmessage);
                $newmessage = str_replace("{city}", $city, $newmessage);
                $newmessage = str_replace("{purchased}", $purchased, $newmessage);
            echo "<div id='msg1'><label>Antwoord:</label><div id='lastmsg'>".nl2br($newmessage)."</div></div>";
            // echo json_encode($newmessage);
        }   
    }
    mysql_free_result($result);
}
else if($dataoption=='table'){
    $query    = "SELECT `tablename` FROM `tablelist`";
    $result   = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    if($num_rows == 0)
        echo json_encode (false);
    else
    {
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            echo "<option value='".$row[0]."'>".$row[0]."</option>";
        }   
    }
}
?>
	
